/**
 * @file basetype.h
 * @author YangSL (dalin@main.com)
 * @brief 
 * @version 0.0.1
 * @date 2022-09-23
 * 
 * @copyright Copyright (c) 2022
 * 
 */

#ifndef __BASETYPE_H__
#define __BASETYPE_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>

typedef unsigned char   uint8;
typedef signed char     sint8;
typedef unsigned short  uint16;
typedef signed short    sint16;
typedef unsigned int    uint32;
typedef signed int      sint32;
typedef long long       sint64;
typedef unsigned long long uint64; 
typedef float           fltp32;
typedef double          fltp64;      

#ifndef __cplusplus
#ifndef bool
#define bool char
#ifndef false
#define false 0
#endif
#ifndef true
#define true 1
#endif
#endif
#endif

#ifndef NULL
#define NULL ((void*)0)
#endif

typedef enum
{
    RC_OK          =  0,
    RC_ERROR       = -1,
    RC_TIMEOUT     = -2
}RETURN_CODE_E;

enum {
    E_RET_SUCCESS = 0 ,
    E_RET_FAIL ,
    E_RET_OVER_RANGE ,
    E_RET_INVALID,
    E_ERT_OTA_NO2APP
};

/* 定义常用宏 */
#define UNUSED_PARAM(x) {(void)(x);}
#define TO_STR(x)       #x
#define CAT_STR(x,y)    x##y
#define DIGIT2ASCII(x)  (((x)>=0x0A)?((x)-0x0A+'A'):((x)+'0'))
#define ASCII2DIGIT(x)  (((x)>='a')?((x)-'a'+0x0A):(((x)>='A')?((x)-'A'+0x0A):((x)-'0')))

#define MAX(a,b)        (((a)>(b))?(a):(b))
#define MIN(a,b)        (((a)<(b))?(a):(b))
#define CLIP(min,x,max) (((x)>(max))?(max):(((x)<(min))?(min):(x)))
#define ABS(a,b)        (((a)>(b))?(a-b):(b-a))
#define EPS             1e-6  //double类型判断零值大小
#define M_PI 3.14159265358979   /**< double型,8字节,15~16位有效数字 */
#define ANG2RAD(angle)  ((angle)*M_PI / 180)    /**< 角度转弧度 */
#define RAD2ANG(radian) ((radian)*180 / M_PI)   /**< 弧度转角度 */

#define BITMAP_GET(value,bit)  (!!((1u<<(bit))&(value)))   /**< 获取value的第bit位的值 */
#define BITMAP_SET(value,bit)  ((1u<<(bit))|(value))       /**< 设置value的第bit位的值 */
#define BITMAP_CLR(value,bit)  ((~(1u<<(bit)))&(value))    /**< 清除value的第bit位的值 */

// host to net short
#define htons(x) ((((x) << 8) & 0xff00) | (((x) >> 8) & 0x00ff))

// host to net long
#define htonl(x) ((((x) << 24) & 0xff000000) | \
                  (((x) << 8) & 0x00ff0000) |  \
                  (((x) >> 8) & 0x0000ff00) |  \
                  (((x) >> 24) & 0x000000ff))

// net to host short
#define ntohs(x) htons(x)

/* 当condition为true,则忙等,直至timeout */
#define WAIT_ON(condition,timeout)  {int i=timeout;while(condition){i--;if(i<=0){goto TIMEOUT;}}}

#endif

